/*
 * 
 * 	userAnswer
 * 	可以获得用户的单选和判断：例：ABCD其中一个，只要和当前题目答案进行比对
 * 	可以获得用户的多选格式：AB A AD ABCD
 * 
 */
export default {
	data() {
		return {
			typeStr:{
				'1': '判断题',
				'2': '单选题',
				'3': '多选题',
				'4': '填空题',
				'5': '问答题'
			},
			popData: {
				flag: false,
				title: '',
				content: ''
			},
			showAnswer: [], //是否已解答
			currentType: 0, //当前题型
			subjectIndex: 0, //跳转索引,也表示当前第几题，从0开始
			autoRadioNext: true, //判断题、单项题，自动移下一题
			// 数据组成：题目，类型，选项，答案，是否回答，是否收藏，答案解析
			subjectList: null,
			dataFlag: false,
			modalCard: null //显示答题卡
		}
	},
	onLoad() {
		this.getSubject();
	},
	onPullDownRefresh() {
		this.getSubject();
		uni.stopPullDownRefresh();
	},
	methods: {
		OpenPop(title, content){
			this.popData.flag = true;
			this.popData.title = title;
			this.popData.content = content;
		},
		HidePop(){
			this.popData.flag = false;
		},
		getSubject: function() {
			this.dataFlag = false;
			// 准备云数据
			// // promise方式
			// uniCloud.callFunction({
			//     name: 'test',
			//     data: { a: 1 }
			//   })
			//   .then(res => {});

			// callback方式
			uniCloud.callFunction({
				name: 'getAllSubject',
				data: {},
				success: (res) => {
					console.log(res.result.data);
					this.showSubject(res.result.data);
				},
				fail: (res) => {
					console.log('fail:' + res);
				},
				complete: (res) => {
					console.log('complete:' + res);
				}
			});
		},
		showSubject: function(data) {
			// 初始化第一题
			this.currentType = data[0].type;

			// 把当前页面标题改变
			// uni.setNavigationBarTitle({
			// 	title: this.title
			// });

			// 动态添加用户显示答案字段，默认全部隐藏
			for (var i = 0; i < data.length; i++) {
				// 采用此方法传入的数据当更新时，视图页面不会刷新，要各种操作才刷新
				// this.subjectList[i].showAnswer = false;

				// 添加的字段数据变化后，视图也会立马刷新，类似加入了监听 
				// userAnswer判断有无回答过，答题卡使用
				this.$set(data[i], "userAnswer", '');
				this.showAnswer.push(false);

			}
			this.dataFlag = true;
			this.subjectList = data;
		},
		showCardModal: function(e) {
			this.modalCard = e.currentTarget.dataset.target
		},
		hideCardModal: function(e) {
			this.modalCard = null
		},
		SwiperChange: function(e) { //滑动事件

			let index = e.target.current;

			// 改变索引（题目显示），当前题型（标题显示）
			if (index != undefined) {
				this.subjectIndex = index;
				this.currentType = this.subjectList[index].type;
			}
		},
		RadioboxChange: function(e) { //单选选中，自动跳转下一题

			// 拿到当前题目选项
			var subject = this.subjectList[this.subjectIndex];
			
			var values = e.detail.value;
			subject.userAnswer = values;
			
			// 单选，判断自动跳下一题
			if (this.autoRadioNext && this.subjectIndex < this.subjectList.length - 1) {
				this.subjectIndex += 1;
			};

		},
		CheckboxChange: function(e) { //复选选中

			var items = this.subjectList[this.subjectIndex].optionList;
			var values = e.detail.value;
			this.subjectList[this.subjectIndex].userAnswer = "";
			for (var i = 0, lenI = items.length; i < lenI; ++i) {
				for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
					if (items[i].id == values[j]) {

						this.subjectList[this.subjectIndex].userAnswer += items[i].id;
						break
					}
				}
			}
		},
		textInput: function(e) { //填空题

			this.subjectList[this.subjectIndex].userAnswer = e.detail.value;

		},
		ShowAnswerChange: function(e) { //显示答案

			// 更新数组立即刷新视图不能通过直接赋值，需要此方法才会立即刷新视图
			// 错误写法,视图如果用此参数判断则不会立即刷新视图：this.showAnswer[]this.subjectIndex] = false;
			if (this.showAnswer[this.subjectIndex]) {
				this.$set(this.showAnswer, this.subjectIndex, false);
			} else {
				this.$set(this.showAnswer, this.subjectIndex, true);
			}
		},

		MoveSubject: function(e) { //上一题、下一题

			if (e === -1 && this.subjectIndex != 0) {
				this.subjectIndex -= 1;
			}
			if (e === 1 && this.subjectIndex < this.subjectList.length - 1) {
				this.subjectIndex += 1;
			}
		},

		AppointedSubject: function(e) { //题卡指定

			this.modalCard = null;
			this.subjectIndex = e;
		},
		ShowToast(msg) {
			uni.showToast({
				title: msg,
				duration: 2000
			});
		},
		SubmitPaper: function(e) { //提交考卷
			let count = 0;
			let error = ''
			for(let i=0; i<this.subjectList.length; i++) {
				
				if (this.subjectList[i].userAnswer.length === 0){
					this.ShowToast('题目还没做完呢!');
					return;
				}
				
				if (this.subjectList[i].userAnswer === this.subjectList[i].answer) {
					count++;
				} else {
					error = error + (i+1) + ' ';
				}
			}
			
			if (error.length === 0) {
				this.OpenPop('成绩:' + count  + '分', '你真棒！');
			} else {
				this.OpenPop('成绩:' + count  + '分', '错误的题目为：' + error);
			}
			
		}
	}
}
